package liguo.views;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

/**
 * @author : Extends 18:07
 * @description : 像Excel表格一样的面板
 * @email : 2563892038@qq.com
 * @date : 2017/4/25
 * @change :
 * @changer :
 */

public class ExcelPanel extends RelativeLayout {

    private RecyclerView topRecycler,leftRecycler,contentRecycler;
    private ScrollView scrollView;
    private RecyclerView.LayoutManager rlm;
    private View view;
    private int mTouchSlop,mMaxFlingVelocity,mMinFlingVelocity;

    public ExcelPanel(Context context) {
        this(context,null);
    }

    public ExcelPanel(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ExcelPanel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initRecycler();
    }
    int todH = 0,todW=0;

    private void initRecycler() {

        final ViewConfiguration vc = ViewConfiguration.get(getContext());
        mTouchSlop = vc.getScaledTouchSlop();
        mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
        mMinFlingVelocity = vc.getScaledMinimumFlingVelocity();

        initTopRecycler();
        initLeftRecycler();
        initScroll();

        leftRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                todH += dy;
                scrollView.smoothScrollTo(0,todH);
            }
        });

        topRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                todW += dx;
                contentRecycler.scrollBy(dx,dy);
            }
        });
    }

    private void initScroll() {
        scrollView = new ScrollView(getContext());
        addView(scrollView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        initContentRecycler();
    }

    private void initContentRecycler() {
        contentRecycler = new RecyclerView(getContext());
        contentRecycler.setLayoutManager(rlm);
        scrollView.addView(contentRecycler, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    private void initTopRecycler() {
        topRecycler = new RecyclerView(getContext());
        topRecycler.setLayoutManager(getTopLayoutManager());
        addView(topRecycler, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }

    private void initLeftRecycler() {
        leftRecycler = new RecyclerView(getContext());
        leftRecycler.setLayoutManager(getLeftLayoutManager());
        addView(leftRecycler, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
    }

    /**
     * 设置内容RecyLayoutManager
     * @param rlm
     */
    public void setAdapter(RecyclerView.LayoutManager rlm,BaseRecyclerAdapter conAdapter,BaseRecyclerAdapter topAdapter,BaseRecyclerAdapter leftAdapter,int w,int h) {
        this.rlm = rlm;
        this.contentRecycler.setLayoutManager(rlm);
        contentRecycler.setAdapter(conAdapter);
        topRecycler.setAdapter(topAdapter);
        leftRecycler.setAdapter(leftAdapter);
        initRule(w,h);
    }

    private void initRule(int w,int h) {
        RelativeLayout.LayoutParams rlp = (LayoutParams) leftRecycler.getLayoutParams();
        rlp.setMargins(0,h,0,0);
        leftRecycler.setLayoutParams(rlp);

        rlp = (LayoutParams) scrollView.getLayoutParams();
        rlp.setMargins(w,h,0,0);
        scrollView.setLayoutParams(rlp);

        rlp = (LayoutParams) topRecycler.getLayoutParams();
        rlp.setMargins(w,0,0,0);
        topRecycler.setLayoutParams(rlp);
    }

    public RecyclerView.LayoutManager getTopLayoutManager() {
        if(topRecycler == null || topRecycler.getLayoutManager()==null){
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            return layoutManager;
        }
        return topRecycler.getLayoutManager();
    }

    public RecyclerView.LayoutManager getLeftLayoutManager() {
        if(leftRecycler == null || leftRecycler.getLayoutManager()==null){
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            return layoutManager;
        }
        return leftRecycler.getLayoutManager();
    }

    public RecyclerView getTopRecycler(){
        return topRecycler;
    }

    public RecyclerView getLeftRecycler(){
        return leftRecycler;
    }

    public RecyclerView getContentRecycler(){
        return contentRecycler;
    }

    private float x,y,newx,newy,oldx=-1000000,oldy=-1000000;
    private int ix,iy;
    private VelocityTracker mVelocityTracker = null;

    private boolean mScrolling;
    private float touchDownX,touchDownY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchDownX = event.getX();
                touchDownY = event.getY();
                mScrolling = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(touchDownX - event.getX()) >= mTouchSlop) {
                    mScrolling = true;
                } else if(Math.abs(touchDownY - event.getY()) >= mTouchSlop){
                    mScrolling = true;
                }else {
                    mScrolling = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                mScrolling = false;
                break;
        }
        return mScrolling;
    }

    float x1 = 0;
    float x2 = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final MotionEvent vtev = MotionEvent.obtain(event);

        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        boolean eventAddedToVelocityTracker = false;

        switch (event.getAction()){
            case MotionEvent.ACTION_UP:
                newx = event.getX();
                newy = event.getY();
                //惯性滑动
                mVelocityTracker.addMovement(vtev);
                eventAddedToVelocityTracker = true;
                mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
                final float xvel = -mVelocityTracker.getXVelocity();
                final float yvel = -mVelocityTracker.getYVelocity();

                leftRecycler.fling((int) xvel, (int) yvel);
                topRecycler.fling((int) xvel, (int) yvel);
//                leftRecycler.invalidate();
//                topRecycler.invalidate();

                oldx = -1000000;
                oldy = -1000000;
            case MotionEvent.ACTION_DOWN:
                x = event.getX();
                y = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                newx = event.getX();
                newy = event.getY();
                if(oldx== -1000000&&oldy==-1000000){
                    oldx = newx;
                    oldy = newy;
                }
                ix = (int)(oldx-newx);
                iy = (int)(oldy-newy);
                leftRecycler.scrollBy(0,iy);
                topRecycler.scrollBy(ix,0);
                oldx = newx;
                oldy = newy;
                break;
        }
        if (!eventAddedToVelocityTracker) {
            mVelocityTracker.addMovement(vtev);
        }
        vtev.recycle();

        return super.onTouchEvent(event);
    }
}
